<?php
namespace Admin\Controller;
use Think\Controller;
class MenuController extends AdminController {
    public function index(){
        $Menu = M('Menu');//实例化AdminUser对象
        $data = $Menu->order('sort asc,u_time desc,id desc')
            ->select();
        $data = get_category($data,0);
        $this->assign('title','菜单栏列表');//标题
        $this->assign('data',$data);// 赋值数据集
        $this->display();
    }

    public function add(){
        if(IS_POST){
            $data = I('post.');
            $Menu = D('Menu');
            $Menu->setData($data);
            $res = $Menu->insertDb();
            if($res){
                $this->success('添加成功',U('index'));
            }else{
                $this->error('添加失败：'.$Menu->getError());
            }
        }else{
            $Menu = M('Menu');
            $data = $Menu->order('sort asc,u_time desc,id desc')
                ->select();
            $data = get_category($data,HOME_PID);
            $this->assign('home_pid',HOME_PID);
            $this->assign('column_arr',$data);// 赋值数据集
            $this->assign('title','菜单栏添加');//标题
            $this->display();
        }
    }


    public function edit($id = 0){
        if(IS_POST){
            $data = I('post.');
            $data['is_show'] = I('post.is_show',0);
            $data['is_blank'] = I('post.is_blank',0);
            $AdminUser = D('Menu');
            $AdminUser->setData($data);
            $res = $AdminUser->updateDb();
            if($res){
                $this->success('修改成功',U('index'));
            }else{
                $this->error('修改失败：'.$AdminUser->getError());
            }
        }else{
            $Menu = M('Menu');
            $one  = M('Menu')->find($id);
            $data = $Menu->order('sort asc,u_time desc,id desc')
                ->select();
            $data = get_category($data,HOME_PID);
//
//            dump($data);
//            exit;
            $this->assign('home_pid',HOME_PID);
            $this->assign('one',$one);
            $this->assign('column_arr',$data);
            $this->assign('title','菜单栏修改');//标题
            $this->display();
        }
    }


    public function delete($id){
        if($id<1){
            $this->error('参数错误,至少选择一条数据');
        }
        // var_dump($id);exit;
        if($id < 1){
            $this->error('参数错误,至少选择一条数据');
        }
        $one = M('Menu')->field('id')->where('pid = '.$id)->find();
        if($one){
            $this->error('删除失败,请先删除该菜单栏下面的所有子菜单栏',U('index'));
        }
        $res = M('Menu')->delete($id);
        if($res !== false){
            $this->success('删除成功',U('index'));
        }else{
            $this->error('删除失败',U('index'));
        }
    }

}


